package com.tianbing.adlottery.controller.servantadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.dao.UserDao;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.User;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class UpdateServantPrimaryCategoryController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UpdateServantPrimaryCategoryController.class.getName());

	private CategoryDao categoryDao;

	private SkinUtils skinUtils;

	private UserDao userDao;

	private UserBusiness userBusiness;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		long childCatCounts = 0;
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		Map model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, null);

		User user = userDao.find(auth.getUserSysId());
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		Category cat = categoryDao.find(catid);
		if (user != null && cat != null) {
			childCatCounts = categoryDao.getChildCategoryCount(cat.getSysId());
			if (childCatCounts == 0) {//only the category is leaf node
				Category oldCat = user.getServantPrimaryAreaCategory();
				if (oldCat != null) {
					userBusiness.removeServantFromCategoryTrail(user.getSysId(), oldCat.getSysId());
				}
				user.setServantPrimaryAreaCategory(cat);
				userDao.save(user);
				userBusiness.addServant2CateogryTrail(user.getSysId(), cat.getSysId());
			}
		}
		model.put("catid", catid);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		if (catid == "") {
			catid = BusinessConstant.CategoryID.AREA_CATEGORY_TOP.getId();
		}
		if (!catid.equals("")) {
			Category cat = categoryDao.find(catid);

			if (cat != null) {
				model.put("category", cat);
				List primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(cat.getSysId());
				List<Category> childCategoryList = categoryDao.getChildCategoryList(cat.getSysId(), 0, 100, null);
				model.put("primaryCategoryTrail", primaryCategoryTrail);
				model.put("childCategoryList", childCategoryList);
			}
		}

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		User user = userDao.find(auth.getUserSysId());

		Category areaCat = user.getServantPrimaryAreaCategory();
		if (areaCat != null) {
			List<Category> servantPrimaryAreaCategoryTrail = categoryDao.getPrimaryCategoryTrail(areaCat.getSysId());
			model.put("servantPrimaryAreaCategoryTrail", servantPrimaryAreaCategoryTrail);
		}

		return model;
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		UpdateServantPrimaryCategoryController.logger = logger;
	}

	/**
	 * @param categoryDao the categoryDao to set
	 */
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @param userBusiness the userBusiness to set
	 */
	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}
}
